<template>
  <div>
    <el-form
      :model="ruleForm"
      status-icon
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item label="用户名/手机号" prop="userName">
        <el-input
          type="test"
          v-model="ruleForm.userName"
          autocomplete="off"
          style="width: 300px"
        ></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="userPwd">
        <el-input
          type="password"
          v-model="ruleForm.userPwd"
          autocomplete="off"
          style="width: 300px"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')"
          >登录</el-button
        >
        <el-button type="primary" @click="resetForm('ruleForm')"
          >重置</el-button
        >
        <el-button type="primary" @click="regis()">注册</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import register from "./register";
export default {
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入用户名/手机号"));
      } else {
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        userName: "",
        userPwd: "",
      },
      baseUrl: "http://localhost:88/api",
      rules: {
        userName: [{ validator: validatePass, trigger: "blur" }],
        userPwd: [{ validator: validatePass2, trigger: "blur" }],
      },
    };
  },
  methods: {
    regis() {
      this.$router.push("/register");
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$axios
            .post(`${this.baseUrl}/pms/user/login`,this.ruleForm)
            .then(({ data }) => {
              console.log(data.user);
              if (data && data.code === 0) {
                localStorage.setItem("userId",data.user.userId);
                localStorage.setItem("userName",data.user.userName);
                this.$router.push("/activity");
              } else {
                this.dataList = [];
                this.totalPage = 0;
              }
              this.dataListLoading = false;
            });
          alert("submit!");
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>